package org.jetnuke.modules.calendar;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.CalendarEventDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.CalendarEvent;

import org.jetnuke.modules.calendar.utils.CalendarUtils;

public class DayCalendar implements IAction {
	int year=0;
	int month=0;
	int day=0;
	public void setDay(int day) {
		this.day = day;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		CalendarUtils.putCommonObjects(vc);
		if(year==0 || month==0 || day==0){
			GregorianCalendar today=new GregorianCalendar();
			year=today.get(Calendar.YEAR);
			month=today.get(Calendar.MONTH)+1;
			day=today.get(Calendar.DAY_OF_MONTH);
		}
		GregorianCalendar day_=new GregorianCalendar(year,month-1,day);
		vc.put("day",day_);
		GregorianCalendar previousDay=new GregorianCalendar();
		previousDay.setTimeInMillis(day_.getTimeInMillis());
		previousDay.add(Calendar.DATE, -1);
		GregorianCalendar nextDay=new GregorianCalendar();
		nextDay.setTimeInMillis(day_.getTimeInMillis());
		nextDay.add(Calendar.DATE, 1);
		vc.put("previousDay",previousDay);
		vc.put("nextDay",nextDay);
		CalendarEventDAO cedao=DataAccessDriver.getInst().newCalendarEventDAO();
		List dayEvents=cedao.selectDayEvents(ui.getID(),day_);
		
		List recurringEvents=cedao.selectRecurringEvents(ui.getID(),day_);
		
		List realRecurringEvents=CalendarUtils.getActiveRecurringEvent(recurringEvents, day_);
		dayEvents.addAll(realRecurringEvents);
		Collections.sort(dayEvents);
		int minHour=24;
		int maxHour=0;
		for(int i=0;i<dayEvents.size();i++){
			CalendarEvent evt=(CalendarEvent)dayEvents.get(i);
			Date time=evt.getSTART_TIME();
			if(time!=null){
				GregorianCalendar gc=new GregorianCalendar();
				gc.setTime(time);
				minHour=gc.get(Calendar.HOUR_OF_DAY);
				break;
			}
		}
		for(int i=dayEvents.size()-1;i>-1;i--){
			CalendarEvent evt=(CalendarEvent)dayEvents.get(i);
			Date time=evt.getSTART_TIME();
			if(time!=null){
				GregorianCalendar gc=new GregorianCalendar();
				gc.setTime(time);
				maxHour=gc.get(Calendar.HOUR_OF_DAY);
				break;
			}
		}
		vc.put("minHour",new Integer(minHour));
		vc.put("maxHour",new Integer(maxHour));
		vc.put("dayEvents", dayEvents);
		return new ActionForward(op,info);
	}

}
